#Matan Keidar 066500992
#Eliahu Khalastchi 040854705

class Corpus(object):
    '''
    represents a corpus of unique words and their mode
    '''
    def S(self):
        '''
        Gets the total size of words that were added to the corpus
        '''
        
        if self.s == 0:
            self.s=sum(self.dic.values())
        return self.s

    def X(self):
        '''
        Gets the size of unique words that were added to the corpus
        '''
        
        if self.x == 0:
            self.x=len(self.dic)
        return self.x
   
    def __init__(self, fileName=None):
        '''
        Constructor: gets an optional file name and fill the corpus
        '''
        self.dic = {}        
        self.s = 0
        self.x = 0
        #check if input file was given as argument
        if fileName is None:
            return
        
        #open input file
        input = open(fileName, "r");
        
        #add each word in input to the corpus
        for line in input:
            words = line.strip().split()
            
            for word in words: 
                self.addWord(word);
                
        input.close()
                    
    def addWord(self, word):
        '''
        Adds a new word to the corpus
        '''
        if self.dic.get(word) is None:
            self.dic[word] = 0
        
        self.dic[word] += 1 

    def C(self, word):
        '''
        Gets the mode of the given word
        '''
        data = self.dic.get(word)
        
        if data is None:
            return 0
        return data
    
    def getEntries(self):
        '''
        Gets a collection of words that are stored in the corpus
        '''        
        return self.dic.keys()
    
    def getValues(self):
        '''
        Gets a collection of values that are stored in the corpus
        '''
        return self.dic.values()
    
    def filterByMode(self, r):
        '''
        Gets a collection of words that have a mode of the given argument
        '''
        return dict(filter(lambda x: x[1] == r, self.dic.items()))
    
    def getWord(self, word):
        '''
        Gets a the raw data of a word from the dictionary
        '''        
        return self.dic.get(word)
    
